using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The CategoryTypeCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about CategoryTypeCodeTable objects.
/// </summary>
public class CategoryTypeCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of CategoryTypeCodeTable from the underlying datasource.
/// </summary>
/// <param name="categoryTypeCode">The unique CategoryTypeCode of the CategoryTypeCodeTable in the database.</param>
/// <returns>An CategoryTypeCodeTable when the CategoryTypeCode was found in the database, or null otherwise.</returns>
public static CategoryTypeCodeTable GetItem(Int16 categoryTypeCode)
{
CategoryTypeCodeTable myCategoryTypeCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCategoryTypeCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@categoryTypeCode", categoryTypeCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myCategoryTypeCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myCategoryTypeCodeTable;
}
}

/// <summary>
/// Returns a list with CategoryTypeCodeTable objects.
/// </summary>
/// <returns>A generics List with the CategoryTypeCodeTable objects.</returns>
public static CategoryTypeCodeTableList GetList()
{
CategoryTypeCodeTableList tempList = new CategoryTypeCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCategoryTypeCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a CategoryTypeCodeTable in the database.
/// </summary>
/// <param name="myCategoryTypeCodeTable">The CategoryTypeCodeTable instance to save.</param>
/// <returns>The new CategoryTypeCode if the CategoryTypeCodeTable is new in the database or the existing CategoryTypeCode when an item was updated.</returns>
public static Int16 Save(CategoryTypeCodeTable myCategoryTypeCodeTable)
{
    Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCategoryTypeCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myCategoryTypeCodeTable.CategoryTypeCode == -1)
{myCommand.Parameters.AddWithValue("@categoryTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@categoryTypeCode", myCategoryTypeCodeTable.CategoryTypeCode);
}
if (myCategoryTypeCodeTable.Description == null){
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myCategoryTypeCodeTable.Description);
}
if (myCategoryTypeCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myCategoryTypeCodeTable.Status);
}
if (myCategoryTypeCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myCategoryTypeCodeTable.CreatedDateTime);
}
if (myCategoryTypeCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myCategoryTypeCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a CategoryTypeCodeTable from the database.
/// </summary>
/// <param name="categoryTypeCode">The CategoryTypeCode of the CategoryTypeCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int16 categoryTypeCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCategoryTypeCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@categoryTypeCode", categoryTypeCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the CategoryTypeCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static CategoryTypeCodeTable FillDataRecord(IDataRecord myDataRecord )
{
CategoryTypeCodeTable myCategoryTypeCodeTable = new CategoryTypeCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CategoryTypeCode")))
{
myCategoryTypeCodeTable.CategoryTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CategoryTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myCategoryTypeCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myCategoryTypeCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myCategoryTypeCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myCategoryTypeCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myCategoryTypeCodeTable;
}
}
